package com.aaa.cs.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.CarDao;
import com.aaa.cs.entity.Car;
import com.aaa.cs.service.CarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Car)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 15:44:18
 */
@Service("carService")
public class CarServiceImpl implements CarService {
    @Resource
    private CarDao carDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carDao.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Car> queryAllByLimit(int offset, int limit) {
        return this.carDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car insert(Car car) {
        this.carDao.insert(car);
        return car;
    }

    /**
     * 修改数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car update(Car car) {
        this.carDao.update(car);
        return (Car) this.queryById(car.getId()).getData();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carDao.deleteById(id) > 0;
    }

    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, Car car) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Car> pageInfo = new PageInfo<Car>(carDao.queryAll(car));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    @Override
    public Result deleteByCarId(Integer id) {
        Car car = new Car();
        car.setId(id);
        car.setState(4);
        int i = this.carDao.update(car);
        if (i > 0){
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(),"删除成功");
        }else {
            return new Result(ReturnCode.FAIL.getCode(),
                    ReturnCode.FAIL.getMsg(),"删除失败");
        }
    }

    /**
     * 新增或修改数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Result insertOrUpdate(Car car) {
        int i = 0;
        if (car.getId() != null && car.getId() !=0) {
            i = carDao.update(car);
            if (i>0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"修改成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"修改失败");
            }
        }else {
            car.setState(1);
            i = carDao.insert(car);
            if (i >0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"添加成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"添加失败");
            }
        }
    }

    @Override
    public Result queryAll(Car car) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.carDao.queryAll(car));
    }
}